/*
 * TtAjaxBinder.scala
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/*
 * BinderMethod.scala
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package net.liftweb.widgets.trees.util

import net.liftweb.widgets.trees.model.TtAjaxHelpers

import scala.collection.mutable.HashMap
import scala.xml.{NodeSeq, Node, PCData, Text, Unparsed, NodeBuffer, Elem}

import java.lang.reflect.Method


class TtAjaxBinder (ttAjaxHelpers : TtAjaxHelpers [Nothing] *) {
  // map the type of row with the "row binder" object and method
  var mapMethods = new HashMap [
      String, 
      Tuple4 [
        TtAjaxHelpers [Nothing], 
        Method, // binder method
        Method, // id method
        Method  // finder method
      ]
    ] ()

  // the row-type class is the second parameter of the render method
  for (ttAjaxHelper <- ttAjaxHelpers; 
           methodBinder <- ttAjaxHelper.binder.getClass.getMethods 
             if (methodBinder.getName == "apply") &&
               methodBinder.getParameterTypes.length == 2 &&
               methodBinder.getParameterTypes.apply (0).getName != "java.lang.Object"
           )
  {
    val rowType = BinderMethod.rowTypeKey (methodBinder.getParameterTypes.apply (1))
    
    val methodId = (for (method <- ttAjaxHelper.id.getClass.getMethods
                        if (method.getName == "apply" && 
                           method.getParameterTypes.length == 1 &&
                           method.getParameterTypes.apply (0).getName != "java.lang.Object")
                       ) yield method).apply (0)
    
    val methodFind = (for (method <- ttAjaxHelper.finder.getClass.getMethods
                        if (method.getName == "apply" && 
                           method.getParameterTypes.length == 1 &&
                           method.getParameterTypes.apply (0).getName != "java.lang.Object")
                       ) yield method).apply (0)
    
    mapMethods += (rowType -> (ttAjaxHelper, methodBinder, methodId, methodFind))
  }
        
  println (mapMethods)
  
  /**
   *  search the method that takes a row and a template as input and returns a xhtml
   */

  def getBinderMethod (rowtype : String) = {
    mapMethods (rowtype)._2
  }

  def getIdMethod (rowtype : String) = {
    mapMethods (rowtype)._3
  }
  
  def getMethods (rowtype : String) = {
    mapMethods (rowtype)
  }
}
